<script setup lang="ts">
import { inject } from 'vue'
import HouseCard from '@/components/common/HouseCard/HouseCard.vue'
import VirtualList from '@/components/common/VirtualList/VirtualList.vue'
import type { ProjectInfo } from '@/api/project'
import type { HouseInfo } from '@/mock/house'
import type { WXMiniProgramProxy } from '@/plugins/wxMiniProgramProxy/wxMiniProgramProxy'

const wxMiniProgramProxy = inject('wxMiniProgramProxy') as WXMiniProgramProxy

interface Props {
  projectInfo: ProjectInfo
  houseList: HouseInfo[]
  loading: boolean
  contentDraggable?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  contentDraggable: true
})

// 点击查看全部
const handleViewAll = () => {
  console.log('查看全部')
  console.error('>>>>此处设置【查看全部】点击后，小程序跳转的目标页path>>>>>>>')

  const currentProjectId = props.projectInfo.id
  wxMiniProgramProxy.navigateTo(`/pages/allHouseList/index?project_id=${currentProjectId}`)
}

// 点击了房源列表中的item
const handleHouseCardClick = (item: HouseInfo) => {
  console.error('>>>>此处配置【房源item】点击后，小程序跳转的目标页path>>>>>>>')

  const house_id = item.id
  wxMiniProgramProxy.navigateTo(`/pages/houseDetail/index?house_id=${house_id}`)
}
</script>

<template>
  <van-floating-panel class="floating-panel" v-bind="$attrs" :duration="0.5">
    <!-- 标题区域 -->
    <div class="header-section">
      <h2 class="main-title">{{ projectInfo.title }}</h2>
      <div class="location-info">
        <span class="location">{{ projectInfo.subtitle }}</span>
        <span class="price-info">{{ projectInfo.description }}</span>
      </div>
    </div>

    <!-- 统计信息区域 -->
    <div class="stats-section">
      <span class="total-count">
        共<span class="count-number">{{ projectInfo.totalCount }}</span>
        套在售
      </span>
      <span class="view-all" @click="handleViewAll">
        查看全部
        <van-icon name="arrow" />
      </span>
    </div>

    <!-- 房源列表区域 -->
    <div class="house-list">
      <div v-if="loading" class="loading-container">
        <van-loading>加载中...</van-loading>
      </div>

      <VirtualList
        v-else
        class="list-container"
        :items="houseList"
        :item-width="184"
        :container-height="250"
        :container-width="400"
        :render-ahead="40"
      >
        <template #default="{ item, style }">
          <div :style="style" class="virtual-item">
            <HouseCard :house-info="item" @click="handleHouseCardClick(item)" />
          </div>
        </template>
      </VirtualList>
    </div>
  </van-floating-panel>
</template>
<style lang="scss" scoped>
.floating-panel {
  box-sizing: border-box;
  padding: 12px;
  background: #fff;
  overflow: hidden;

  .header-section {
    margin-bottom: 8px;

    .main-title {
      font-size: 18px;
      font-weight: bold;
      color: #333;
      margin: 0 0 4px 0;
      line-height: 1.3;
    }

    .location-info {
      display: flex;
      align-items: center;
      gap: 12px;

      .location {
        font-size: 13px;
        color: #666;
      }

      .price-info {
        font-size: 13px;
        color: #666;
      }
    }
  }

  .stats-section {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid #f0f0f0;

    .total-count {
      font-size: 14px;
      color: #333;

      .count-number {
        color: #ff6b35;
        font-weight: bold;
      }
    }

    .view-all {
      display: flex;
      align-items: center;
      gap: 4px;
      font-size: 14px;
      color: #666;
      cursor: pointer;

      &:hover {
        color: #ff6b35;
      }
    }
  }

  .house-list {
    position: relative;
    box-sizing: border-box;

    .loading-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 200px;
    }

    .virtual-item {
      height: 100%;
      display: flex;
    }

    .list-container {
      padding: 0 12px;
      margin-bottom: 10px;
    }
  }
}
</style>
